package net.icontents.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.icontents.Constants;
import net.icontents.service.ArticleService;
import net.icontents.service.CatalogService;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

/**
 * 前端页面展示入口
 *
 * @author zhurx
 */
@Controller
@RequestMapping(value="index.do")
public class IndexController extends ParameterizableViewController {
	@Resource
	private ArticleService articleService;
	
	@Resource
	private CatalogService catalogService;
	
	@Override
	protected ModelAndView handleRequestInternal(
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> model = new HashMap<String,Object>();
		model.put("contextPath", request.getContextPath());

		int articleId = ServletRequestUtils.getIntParameter(request, "articleId", Constants.ERROR);
		
		if(articleId!=Constants.ERROR){
			model.put("article", articleService.getArticleById(articleId,true));
		}
		
		model.put("catalogList", catalogService.select(0, Integer.MAX_VALUE));
		return new ModelAndView(getViewName(),model);
	}

}
